﻿using de.wolfmarco.pas.Shared.Properties;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Infrastructure.DependencyResolution;
using System.Data.Entity.Migrations.History;
using System.Data.Entity.Migrations.Sql;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace de.wolfmarco.pas.Shared.Database
{
    public class DynamicDbConfiguration : DbConfiguration
    {
        public DynamicDbConfiguration()
        {
            switch (Settings.Default.DatabaseType)
            {
                case DatabaseTypeEnum.MySQL:
                    SetExecutionStrategy( MySqlProviderInvariantName.ProviderName, () => new MySqlExecutionStrategy() );
                    AddDependencyResolver( new MySqlDependencyResolver() );
                    SetProviderFactory( MySqlProviderInvariantName.ProviderName, new MySqlClientFactory() );
                    SetProviderServices( MySqlProviderInvariantName.ProviderName, new MySqlProviderServices() );
                    SetDefaultConnectionFactory( new MySqlConnectionFactory() );
                    SetMigrationSqlGenerator( MySqlProviderInvariantName.ProviderName, (Func<MySqlMigrationSqlGenerator>) ( () => new MySqlMigrationSqlGenerator() ) );
                    SetProviderFactoryResolver( new MySqlProviderFactoryResolver() );
                    SetManifestTokenResolver( new MySqlManifestTokenResolver() );
                    SetHistoryContext(MySqlProviderInvariantName.ProviderName, (existingConnection, defaultSchema) => new PasMySqlHistoryContext(existingConnection, defaultSchema) );
                    break;

                case DatabaseTypeEnum.Local:
                default:
                    break;
            }
        }
    }
}
